/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package smary.managed.install;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Inject;
import smary.ejb.entity.Rollen;
import smary.ejb.entity.Userloginaccount;
import smary.ejb.stateless.auto.RollenFacade;
import smary.ejb.stateless.auto.UserloginaccountFacade;

/**
 * Nimmt die nötigen Operationen zum einrichten von Scrummary vor. Momentan
 * legt der installService die Rollen fest und den Admin.
 * @author Alexander Cannell
 */
@Named(value = "installService")
@SessionScoped
public class installService implements Serializable {

    private String success = "Nicht Installiert";
    private int token;
    private String inputToken;

    public int getToken() {
        return token;
    }

    public void setToken(int token) {
        this.token = token;
    }

    public String getInputToken() {
        return inputToken;
    }

    public void setInputToken(String inputToken) {
        this.inputToken = inputToken;
    }
    

    public String getSuccess() {
        doInstall();
        return success;
    }

    public void setSuccess(String success) {
        this.success = success;
    }
    private int step = 1;

    public int getStep() {
        return ++step;
    }

    public void setStep(int step) {
        this.step = step;
    }
    
   @Inject
   RollenFacade rfl;
   
   @Inject
   UserloginaccountFacade ufl;


    @Inject
    Userloginaccount adminAcc;

    public Userloginaccount getAdminAcc() {
        return adminAcc;
    }

    public void setAdminAcc(Userloginaccount adminAcc) {
        this.adminAcc = adminAcc;
    }
    
    /** Creates a new instance of installService */
    public installService() {
        token=(int) (Math.random() * 9999); 
        
    }

    public void showToken(){
        
        System.out.println("SICHERHEITSTOKEN:" + token);
    }
    private void doInstall() {
        if(Integer.parseInt(inputToken) == token){
            
        
        createRoles();
        adminAcc.setLoginid(1);
        
        for(Rollen rolle : rfl.findAll()){
            if(rolle.getRollenname().equals("Admin")){
                adminAcc.setRolle(rolle);
            }
        }
        try {
            ufl.create(adminAcc);
        } catch (Exception e) {
        }
        
        success="Erfolg";
        }
    }

    private void createRoles() {
        try {
            rfl.create(new Rollen((short)1,"ScrumMaster"));
        } catch (Exception e) {
        }
        try {
            rfl.create(new Rollen((short)2,"ScrumTeamMitglied"));
        } catch (Exception e) {
        }
        try {
            rfl.create(new Rollen((short)3,"ProductOwner"));
        } catch (Exception e) {
        }
        try {
            rfl.create(new Rollen((short)4,"Qualitätssicherung"));
        } catch (Exception e) {
        }
        try {
            rfl.create(new Rollen((short)5,"Admin"));
        } catch (Exception e) {
        }
    }
}
